package com.sita.tboard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sita.bike.R;
import com.sita.tboard.ui.tools.ScreenTools;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import cn.trinea.android.common.util.ScreenUtils;

public class TrendsGridAdapter extends BaseAdapter {
    private ArrayList<String> mUris;

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public TrendsGridAdapter(ArrayList<String> uris, Context context) {
        mUris = uris;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mUris == null ? 0 : mUris.size();
    }

    @Override
    public String getItem(int position) {
        if (mUris == null || mUris.isEmpty()){
            return null;
        }
        return mUris.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyGridViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new MyGridViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.rt_trends_gridview_item,
                    parent, false);

            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.album_image);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (MyGridViewHolder) convertView.getTag();
        }
        String url = getItem(position);

        if (getCount() == 1) {
            viewHolder.imageView.setLayoutParams(new AbsListView.LayoutParams(400, 200));
        }

        if (getCount() == 2 || getCount() == 4) {
            viewHolder.imageView.setLayoutParams(new AbsListView.LayoutParams(200, 200));
        }

        Picasso.with(mContext.getApplicationContext()).load(url).fit().centerInside()
                .placeholder(R.drawable.empty_photo).into(viewHolder.imageView);

        return convertView;
    }

    class MyGridViewHolder {
        ImageView imageView;
    }
}
